#include "Layout.h"

Layout::Layout()
{
    m_nWins = 0;
    m_sub_wins[0] = NULL;
    m_sub_wins[1] = NULL;
}

Layout::~Layout()
{
}

void Layout::Shift(int len)
{
    if(m_nWins == 2){
        if (m_type == VERT) {
            int tmp = round(m_h * m_split_bar) + len;
            if(tmp > (m_h - m_sub_wins[1]->GetMinH()))
                tmp = m_h - m_sub_wins[1]->GetMinH();      
            if(tmp < (m_sub_wins[0]->GetMinH()))
                tmp = m_sub_wins[0]->GetMinH();
            if(m_h)
                m_split_bar = (double)tmp / (double)m_h;

        } else if(m_type == HORIZ) {
            int tmp = round(m_w * m_split_bar) + len;
            if(tmp > (m_w - m_sub_wins[1]->GetMinW()))
                tmp = m_w - m_sub_wins[1]->GetMinW();      
            if(tmp < (m_sub_wins[0]->GetMinW()))
                tmp = m_sub_wins[0]->GetMinW();
            if(m_w)
                m_split_bar = (double)tmp / (double)m_w;

        }
        Display(true);
    }
}

void Layout::UpdateLayout()
{
    if(m_nWins == 1){
        if(m_sub_wins[0]->IsEnabled())
            m_sub_wins[0]->Locate(m_y, m_x, m_h, m_w);
    }else if (m_nWins == 2) {
        if(m_sub_wins[0]->IsEnabled() && m_sub_wins[1]->IsEnabled()){
            if (m_type == VERT) {
                int tmp = round(m_h * m_split_bar);
                if(tmp < m_sub_wins[0]->GetMinH())
                    tmp = m_sub_wins[0]->GetMinH();
                if(tmp > (m_h - m_sub_wins[1]->GetMinH()))
                    tmp = m_h - m_sub_wins[1]->GetMinH();
                m_sub_wins[0]->Locate(m_y, m_x, tmp, m_w);
                m_sub_wins[1]->Locate((m_y + tmp), m_x, (m_h - tmp), m_w);
            }else if(m_type == HORIZ){
                int tmp = round(m_w * m_split_bar);
                if(tmp < m_sub_wins[0]->GetMinW())
                    tmp = m_sub_wins[0]->GetMinW();
                if(tmp > (m_w - m_sub_wins[1]->GetMinW()))
                    tmp = m_w - m_sub_wins[1]->GetMinW();
                m_sub_wins[0]->Locate(m_y, m_x, m_h, tmp);
                m_sub_wins[1]->Locate(m_y, (m_x + tmp), m_h, (m_w - tmp));
            }
        } else if (m_sub_wins[0]->IsEnabled()){
            m_sub_wins[0]->Locate(m_y, m_x, m_h, m_w);
        } else if (m_sub_wins[1]->IsEnabled()){
            m_sub_wins[1]->Locate(m_y, m_x, m_h, m_w);
        }
    }
}

//Print actual data of the buffer on the window
int Layout::Display(int focus)
{
    if (IsEnabled()) {
        UpdateLayout();
    }
    return 0;
}

int Layout::GetMinH()
{
    m_min_h = 0;
    if (IsEnabled()) {
        if (m_type == VERT) {
            for (int i = 0; i < m_nWins; i++) {
                if(m_sub_wins[i]->IsEnabled())
                    m_min_h += m_sub_wins[i]->GetMinH();
            }
        }else if(m_type == HORIZ){
            for (int i = 0; i < m_nWins; i++) {
                if(m_sub_wins[i]->IsEnabled())
                    m_min_h = (m_sub_wins[i]->GetMinH() > m_min_h)? m_sub_wins[i]->GetMinH() : m_min_h;
            }
        }
    }
    return m_min_h;
}

int Layout::GetMinW()
{
    m_min_w = 0;
    if (IsEnabled()) {
        if (m_type == VERT) {
            for (int i = 0; i < m_nWins; i++) {
                if(m_sub_wins[i]->IsEnabled())
                    m_min_w = (m_sub_wins[i]->GetMinW() > m_min_w)? m_sub_wins[i]->GetMinW() : m_min_w;
            }
        }else if(m_type == HORIZ){
            for (int i = 0; i < m_nWins; i++) {
                if(m_sub_wins[i]->IsEnabled())
                    m_min_w += m_sub_wins[i]->GetMinW();
            }
        }
    }
    return m_min_w;
}

//Updates screen buffer
void Layout::Update()
{
}

//Does actual paint on screen
void Layout::Clean()
{

}

void Layout::Paint()
{

}

void Layout::AddWindow(IWindow* win)
{
    if (m_nWins == 1 || m_nWins == 0 ) {
        IWindow::AddWindow(win);
        m_sub_wins[m_nWins] = win;
        m_nWins++;
        if (m_type == VERT) {
            m_min_h += win->GetMinH();
        }else if(m_type == HORIZ){
            m_min_w += win->GetMinW();
        }
        Display(true);
    }
}

void Layout::RemoveWindow(IWindow* win)
{
    if (m_nWins == 2 || m_nWins == 1) {
        if (win ==  m_sub_wins[1]) {
            m_sub_wins[1] = NULL;
            m_nWins--;
            IWindow::RemoveWindow(win);
            if (m_type == VERT) {
                m_min_h -= win->GetMinH();
            }else if(m_type == HORIZ){
                m_min_w -= win->GetMinW();
            }
            Display(true);
        }
        if(win ==  m_sub_wins[0]){
            m_sub_wins[0] = m_sub_wins[1];
            m_sub_wins[1] = NULL;
            m_nWins--;
            IWindow::RemoveWindow(win);
            if (m_type == VERT) {
                m_min_h -= win->GetMinH();
            }else if(m_type == HORIZ){
                m_min_w -= win->GetMinW();
            }
            Display(true);
        }
    }
}

void Layout::Locate(int y, int x, int h, int w)
{
    IWindow::Locate(y, x, h, w);
    Display(true);
}

void Layout::Resize_LT(int x, int y)
{
    IWindow::Resize_LT(x, y);
    Display(true);
}

void Layout::Resize_RB(int x, int y)
{
    IWindow::Resize_RB(x, y);
    Display(true);
}

void Layout::Move(int x, int y)
{
    IWindow::Move(x, y);
    Display(true);
}

bool Layout::IsEnabled()
{
    for (int i = 0; i < m_nWins; i++) {
        if(m_sub_wins[i]->IsEnabled()){
            if(!IWindow::IsEnabled())
                Enable();
            return true;
        }
    }
    if(IWindow::IsEnabled())
        Disable();

    return false;
}

void Layout::Enable()
{
    IWindow::Enable();
}

void Layout::Disable()
{
    IWindow::Disable();
}

void Layout::Top()
{
    for (int i = 0; i < m_nWins; i++) {
        m_sub_wins[i]->Top();
    }
}
